package org.zxw.controller.trainPlan;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.zxw.pojo.entity.TrainingPlan;
import org.zxw.service.TrainingPlanService;

import java.util.List;

@RestController
@RequestMapping("/api/v1/training/")
@Slf4j
public class TrainingPlanController {
    private final TrainingPlanService trainingPlanService;
    
    public TrainingPlanController(TrainingPlanService trainingPlanService) {
        this.trainingPlanService = trainingPlanService;
    }

    /**
     * 基础推荐接口
     * @param goal
     * @param limit
     * @return
     */
    @GetMapping("/foundation")
    public ResponseEntity<List<TrainingPlan>> recommendPlan(
            @RequestParam String goal,
            @RequestParam(defaultValue = "3") int limit) {
        log.info("接收到基础推荐请求，目标：{}，数量：{}", goal, limit);
        List<TrainingPlan> plans = trainingPlanService.recommendByGoal(goal, limit);
        return ResponseEntity.ok(plans);
    }

    /**
     * 高级推荐接口
     * @param goal
     * @param difficulty
     * @param limit
     * @return
     */
    @GetMapping("/advanced")
    public ResponseEntity<List<TrainingPlan>> recommendPlanAdvanced(
            @RequestParam String goal,
            @RequestParam String difficulty,
            @RequestParam(defaultValue = "3") int limit) {
        log.info("接收到高级推荐请求，目标：{}，难度：{}，数量：{}", goal, difficulty, limit);
        List<TrainingPlan> plans = trainingPlanService.recommendByGoalAndDifficulty(goal, difficulty, limit);
        return ResponseEntity.ok(plans);
    }

    /**
     * 智能推荐接口 -根据BMI和难度
     * @param height
     * @param weight
     * @param difficulty
     * @param limit
     * @return
     */
    @GetMapping("/smart")
    public ResponseEntity<List<TrainingPlan>> recommendPlanSmart(
            @RequestParam double height, // 单位:米
            @RequestParam double weight, // 单位:千克
            @RequestParam(defaultValue = "中级") String difficulty,
            @RequestParam(defaultValue = "3") int limit) {
        log.info("接收到智能推荐请求，身高：{}米，体重：{}千克，难度：{}，数量：{}", height, weight, difficulty, limit);
        // 计算BMI
        double bmi = weight / (height * height);
        List<TrainingPlan> plans = trainingPlanService.recommendByBmi(bmi, difficulty, limit);
        return ResponseEntity.ok(plans);
    }

    /**
     * 刷新缓存
     * @param goal
     * @return
     */
    @PostMapping("/cache/refresh")
    public ResponseEntity<String> refreshCache(@RequestParam String goal) {
        log.info("接收到刷新缓存请求，目标：{}", goal);
        trainingPlanService.refreshCache(goal);
        return ResponseEntity.ok("缓存刷新成功");
    }
}